package team.tunan.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import team.tunan.common.Result;
import team.tunan.dto.EmployeePageDto;
import team.tunan.entity.Employee;
import team.tunan.entity.Menu;
import team.tunan.service.IEmployeeService;
import team.tunan.service.IMenuService;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author sqx
 * @since 2023-12-24
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;

    @Autowired
    private IMenuService iMenuService;



    @PostMapping("/create")
    public Result insertEmployee(@RequestBody Employee employee){
        return Result.success(iEmployeeService.insertEmployee(employee));
    }

    @PostMapping("/update")
    public Result updateEmployee(@RequestBody Employee employee){
        return Result.success(iEmployeeService.updateEmployee(employee));
    }

    @GetMapping("/delete/{id}")
    public Result deleteEmployee(@PathVariable String id){
        return Result.success(iEmployeeService.deleteEmployee(id));
    }

    @PostMapping("/selectPage")
    public Result<Page<Employee>> selectPage(@RequestBody EmployeePageDto employeePageDto){
        return Result.success(iEmployeeService.selectPage(employeePageDto));
    }

    @GetMapping("/findByUserName")
    public Result findByUserName(@RequestParam String userName){
        return iEmployeeService.findByUserName(userName)?Result.success("success"):Result.fail("该用户名已存在");
    }
    @GetMapping("/selectByEmployeeId/{id}")
    public Result selectByEmployeeId(@PathVariable Integer id ){
        return Result.success(iEmployeeService.selectByEmployeeId(id));
    }

    @PostMapping("/login")
    public Result login(@RequestBody Employee employee){
        Employee employee1=iEmployeeService.login(employee);
        if(employee1!=null){
            List<Menu> menus = iMenuService.listByRole(String.valueOf(employee1.getRole()));
            HashMap res = new HashMap();
            res.put("employee",employee1);
            res.put("menu",menus);
            return Result.success(res);
        }
        return Result.fail("登录失败");
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(iEmployeeService.list());
    }

}

